<template>
  <footer class="footer">
    <span class="todo-count"
      >剩余<strong>{{ restNum }}</strong></span
    >
    <ul class="filters">
      <li>
        <a
          :class="{ selected: curIndex === 0 }"
          @click="changeSel(0)"
          href="javascript:;"
          >全部</a
        >
      </li>
      <li>
        <a
          :class="{ selected: curIndex === 1 }"
          @click="changeSel(1)"
          href="javascript:;"
          >未完成</a
        >
      </li>
      <li>
        <a
          :class="{ selected: curIndex === 2 }"
          @click="changeSel(2)"
          href="javascript:;"
          >已完成</a
        >
      </li>
    </ul>
    <button class="clear-completed" @click="clear">清除已完成</button>
  </footer>
</template>

<script>
export default {
  props: ["list"],
  data() {
    return {
      curIndex: 0,
    };
  },
  methods: {
    // 1.切换 按钮--------------------------------
    changeSel(i) {
      this.curIndex = i;
      this.$emit("onChangeType", i);
    },
    // 2.清空 已完成任务-------------------------
    clear() {
      this.$emit("onClear");
    },
  },
  computed: {
    // 获取 未完成的 任务数量
    restNum() {
      // a.筛选出 未完成的 任务对象，装入新数组 返回
      const undoneArr = this.list.filter((e) => !e.isDone);
      // b.返回 未完成任务的 数量
      return undoneArr.length;
    },
    restNum2() {
      return this.list.reduce((sum, ele) => {
        if (!ele.isDone) {
          return sum + 1;
        } else return sum;
      }, 0);
    },
  },
};
</script>